JavaScript Runtime Inference Notes

Due to the strategy used for runtime rule and command execution, XpertRule procedures or tree-commands written in Javascript code will be executed multiple times during the runtime session life cycle.

Some aspects of your code (e.g. custom user interaction) would only be required to execute the first time they are encountered. To achieve this behaviour, you can use a Critical Section.

Critical Sections

These are sections of code which allow you to perform different behaviour for the first and subsequent times that the engine passes through your procedure. Below are some typical usage scenarios...

1. Code which should only be called the first time your procedure is encountered

A typical usage scenario would be the use of a JavaScript alert i.e. the message should only be shown the first time that it is encountered.
You can achieve this functionality with the following code snippet...
replayStack.CriticalSection(
    function(rs){
        alert("Hello World!");
        rs.CriticalDone();
    }
);

2. Code which requests data from a server

Requesting data from a server tends to be a lenghty operation and should only be executed the first time through your procedure. On subsequent calls, you would typically use the retrieved data to assign XpertRule object values.
A good rule of thumb is...
There follows some code to perform a jQuery ajax call to retireve data from the server and assign this data to an XpertRule object.
replayStack.CriticalSection(
    function(rs){
        // This section of code is only called the first time this procedure is encountered
        $.ajax({
        	url: "getdata.php",            
            type: "GET",
            timeout: 3000,
            data: {
            	userID: #userID.val()         // The server script expects 1 parameter called "userID". We'll send the value of the userID XpertRule Object
            },
            success: function(svrData){
                rs.CriticalDone(svrData);     // The server has responded. Continue inference passing the returned data for future use
            }
        });
    }, function(svrData){
        // This section of code is called multiple times
        #userName.val(svrData.name);          // Assuming the server returns a JSON object with a name property, use the userName XpertRule object to the returned data
    }
);
n.b. For more information on the jQuery ajax method, see the jQuery documentation.

You may notice that the second example is asyncronous (i.e. the $.ajax function returns immediatly with the success function being called at some later time). By placing the CriticalDone call in the success function we have waited for an asyncronous event to occur before continuting rule execution.